let request = require("./request")

//流程定义相关flowable的rest调用
let handler = {
    deployment_create: async function (name, xml) {
        const FormData = require('form-data');
        const form = new FormData();
        const fileContent = Buffer.from(xml);
        form.append('file', fileContent, {
            filename: name+".bpmn",
            contentType: 'text/plain',
            knownLength: fileContent.byteLength
        });

        let result = await request.post_file('repository/deployments', form);
        if (result.code == 201) {
            return result.data;
        }
        else {
            throw({
                name: "发布流程错误",
                message: result.code+" "+result.data.message+" "+result.data.exception
            });
        }
    },
    get_deployment_process_defs: async function (deployment_id) {
        let result = await request.get('repository/process-definitions?deploymentId='+deployment_id);
        if (result.code != 200) {
            throw({
                name: "获取流程部署错误",
                message: result.code+" "+result.data.message+" "+result.data.exception
            });
        }
        else {
            return result.data;
        }
    }
}

module.exports = handler;